package com.zone.domain.strategy.repository;

import com.google.common.collect.Range;
import com.zone.domain.strategy.model.entity.RaffleAwardEntity;
import com.zone.domain.strategy.model.entity.StrategyAwardEntity;
import com.zone.domain.strategy.model.entity.StrategyEntity;
import com.zone.domain.strategy.model.entity.StrategyRuleEntity;
import com.zone.domain.strategy.model.valobj.AwardStockSubTaskVO;
import com.zone.domain.strategy.model.valobj.RuleEnums;
import com.zone.domain.strategy.model.valobj.RuleTreeVO;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author: zongzi
 * @description: 策略相关
 * @date: 2024/5/26
 */
@Repository
public interface IStrategyRepository {
    /**
     * 获取策略明细集合
     *
     * @param strategyId 策略id
     * @return 策略明细集合
     */
    List<StrategyAwardEntity> listStrategyAward(Long strategyId);

    /**
     * 获取策略明细
     *
     * @param strategyId 策略id
     * @param awardId    奖品id
     * @return 奖品策略
     */
    StrategyAwardEntity getStrategyAward(Long strategyId, Integer awardId);

    /**
     * 将计算出的奖品-范围表存入缓存
     *
     * @param key             存入缓存的key
     * @param awardRangeTable 计算出的奖品-范围表
     */
    void storeStrategyAwardTable(String key, Map<Range<Integer>, Integer> awardRangeTable);

    /**
     * 获取策略范围
     *
     * @param key 存入缓存的key
     * @return 策略范围
     */
    Integer getRateRange(String key);

    /**
     * 获取奖品-概率表
     *
     * @param key 存入缓存的key
     * @return 奖品-概率表
     */
    Map<Range<Integer>, Integer> getAwardRangeTable(String key);

    /**
     * 查询策略信息
     *
     * @param strategyId 策略id
     * @return 策略实体
     */
    StrategyEntity getStrategyEntity(Long strategyId);

    /**
     * 查询策略规则集合
     *
     * @param strategyId 策略id
     * @param ruleModel  规则模型
     * @return 策略规则集合
     */
    List<StrategyRuleEntity> queryStrategyRules(Long strategyId, String ruleModel);

    /**
     * 查询规则树
     *
     * @param treeId 树id
     * @return 规则树
     */
    RuleTreeVO getRuleTreeVoByTreeId(String treeId);

    /**
     * 装配奖品库存数量
     *
     * @param strategyId 策略id
     * @param awardId    奖品id
     * @param awardCount 奖品库存总量
     */
    void assembleStockData(Long strategyId, Integer awardId, Integer awardCount);

    /**
     * 渐减奖品剩余库存 strategy_award.award_count_surplus
     *
     * @param strategyId 策略id
     * @param awardId    奖品id
     */
    void decrStrategyAwardCountSurplus(Long strategyId, Integer awardId);

    /**
     * 扣减指定奖品扣减（渐减式扣减 100，99，98...）
     *
     * @param strategyId 策略id
     * @param awardId    奖品id
     * @return 是否扣减库存成功
     */
    boolean descAwardStock(Long strategyId, Integer awardId);

    /**
     * 拉取一条 奖品库存扣减任务
     *
     * @return 奖品库存扣减任务
     */
    AwardStockSubTaskVO pollAwardStockSubTask();

    /**
     * 将奖品库存扣减任务加入到队列中
     *
     * @param subTask 奖品库存扣减任务
     */
    void offerAwardStockSubTask(AwardStockSubTaskVO subTask);

    /**
     * 查询策略规则
     *
     * @param strategyId 策略id
     * @param awardId    奖品id
     * @return
     */
    StrategyRuleEntity queryStrategyAwardRule(Long strategyId, Integer awardId);

    StrategyRuleEntity queryStrategyRule(Long strategyId, RuleEnums.RuleTypeEnum ruleTypeEnum, RuleEnums.RuleModelEnum ruleModelEnum);

}
